package com.ruoyi.statistics.service.impl;

import com.ruoyi.labdata.domain.LabEmployee;
import com.ruoyi.labdata.domain.LabOrgnization;
import com.ruoyi.labdata.service.ILabEmployeeService;
import com.ruoyi.statistics.domain.LabStatisticsBaseCollege;
import com.ruoyi.statistics.domain.LabStatisticsLaboratory;
import java.util.List;
import com.ruoyi.common.utils.DateUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.ruoyi.statistics.mapper.LabStatisticsPersonMapper;
import com.ruoyi.statistics.domain.LabStatisticsPerson;
import com.ruoyi.statistics.service.ILabStatisticsPersonService;

/**
 * 专任实验室人员Service业务层处理
 * 
 * @author jun.wang
 * @date 2022-05-21
 */
@Service
public class LabStatisticsPersonServiceImpl implements ILabStatisticsPersonService 
{
    @Autowired
    private LabStatisticsPersonMapper labStatisticsPersonMapper;

    @Autowired
    private ILabEmployeeService labEmployeeService;

    /**
     * 查询专任实验室人员
     * 
     * @param id 专任实验室人员ID
     * @return 专任实验室人员
     */
    @Override
    public LabStatisticsPerson selectLabStatisticsPersonById(Long id)
    {
        return labStatisticsPersonMapper.selectLabStatisticsPersonById(id);
    }

    /**
     * 查询专任实验室人员列表
     * 
     * @param labStatisticsPerson 专任实验室人员
     * @return 专任实验室人员
     */
    @Override
    public List<LabStatisticsPerson> selectLabStatisticsPersonList(LabStatisticsPerson labStatisticsPerson)
    {
        return labStatisticsPersonMapper.selectLabStatisticsPersonList(labStatisticsPerson);
    }

    /**
     * 新增专任实验室人员
     * 
     * @param labStatisticsPerson 专任实验室人员
     * @return 结果
     */
    @Override
    public int insertLabStatisticsPerson(LabStatisticsPerson labStatisticsPerson)
    {
        return labStatisticsPersonMapper.insertLabStatisticsPerson(labStatisticsPerson);
    }

    /**
     * 修改专任实验室人员
     * 
     * @param labStatisticsPerson 专任实验室人员
     * @return 结果
     */
    @Override
    public int updateLabStatisticsPerson(LabStatisticsPerson labStatisticsPerson)
    {
        labStatisticsPerson.setUpdateTime(DateUtils.getNowDate());
        return labStatisticsPersonMapper.updateLabStatisticsPerson(labStatisticsPerson);
    }

    /**
     * 批量删除专任实验室人员
     * 
     * @param ids 需要删除的专任实验室人员ID
     * @return 结果
     */
    @Override
    public int deleteLabStatisticsPersonByIds(Long[] ids)
    {
        return labStatisticsPersonMapper.deleteLabStatisticsPersonByIds(ids);
    }

    /**
     * 删除专任实验室人员信息
     * 
     * @param id 专任实验室人员ID
     * @return 结果
     */
    @Override
    public int deleteLabStatisticsPersonById(Long id)
    {
        return labStatisticsPersonMapper.deleteLabStatisticsPersonById(id);
    }

    @Override
    public List<LabStatisticsPerson> selectLabStatisticsPersonByBaseCollegeId(Long baseCollegeId) {
        return labStatisticsPersonMapper.selectLabStatisticsPersonByBaseCollegeId(baseCollegeId);
    }

  @Override
  public void addPerson(LabStatisticsBaseCollege baseCollege, List<LabOrgnization> labOrgnizations) {
      Long baseCollegeId = baseCollege.getId();
      deleteLabStatisticsPersonByBaseCollegeId(baseCollegeId);

      for (LabOrgnization labOrgnization : labOrgnizations) {

          LabEmployee query = new LabEmployee();
          query.setOrgId(labOrgnization.getOrgId());
          List<LabEmployee> labEmployees = labEmployeeService.selectLabEmployeeList(query);

          for (LabEmployee labEmployee : labEmployees) {

              LabStatisticsPerson person = new LabStatisticsPerson();
              person.setBaseCollegeId(baseCollegeId);
              person.setLabId(labOrgnization.getOrgId());
              //person.setLabCode(labOrgnization.getOrgCode());
              //改为实验室房间号
              //
              person.setOrgName(labOrgnization.getParentName());
              person.setLabName(labOrgnization.getOrgName());

              person.setUserName(labEmployee.getEmployeeNo());
              person.setName(labEmployee.getEmployeeName());
              person.setSex(labEmployee.getSex());
              person.setBirthday(labEmployee.getBirthday());
              person.setSubject(labEmployee.getMajorStudied());

              insertLabStatisticsPerson(person);
          }

      }

  }

  @Override
  public int deleteLabStatisticsPersonByBaseCollegeId(Long baseCollegeId) {

        return labStatisticsPersonMapper.deleteLabStatisticsPersonByBaseCollegeId(baseCollegeId);
  }
}
